package com.talent.jiwen;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.helper.ViewPagerAdatper;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.util.LogUtil;
import com.talent.jiwen.util.MD5;
import com.talent.jiwen.util.MobileInfoUtil;
import com.talent.jiwen.util.MyToast;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.functions.Consumer;
import rx.Observable;


/**
 * Created by qyqx on 2018/8/15.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener{

    @BindView(R.id.in_viewpager)
    ViewPager vPager;
    @BindView(R.id.img_go)
    ImageView img_go;


    private ViewPagerAdatper vpAdapter;
    private static  int[] imgs = {R.mipmap.guide_1,R.mipmap.guide_2, R.mipmap.guide_3};
    private ArrayList<ImageView> imageViews;


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_guide;
    }

    @Override
    public String titleName() {
        return null;
    }

    @Override
    protected void initData() {
        setHeadVisibility(View.GONE);

//        vpAdapter=new ViewPagerAdatper()

        //设置每一张图片都填充窗口
        ViewPager.LayoutParams mParams = new ViewPager.LayoutParams();
        imageViews = new ArrayList<ImageView>();

        for(int i=0; i<imgs.length; i++)
        {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);//设置布局
            iv.setImageResource(imgs[i]);//为ImageView添加图片资源
            iv.setScaleType(ImageView.ScaleType.FIT_XY);//这里也是一个图片的适配
            imageViews.add(iv);
        }

        vpAdapter=new ViewPagerAdatper(imageViews);
//        vPager.setPageTransformer(true,new DepthPageTransformer());
        vPager.setAdapter(vpAdapter);
        vPager.setOnPageChangeListener(this);

        reqirePermission();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==2){
            img_go.setVisibility(View.VISIBLE);
            img_go.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent toMainActivity = new Intent(GuideActivity.this, LoginActivity.class);//跳转到主界面
                    startActivity(toMainActivity);
                    finish();
                }
            });
        }else{
            img_go.setVisibility(View.GONE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @SuppressLint("checkResult")
    private void reqirePermission(){
        RxPermissions rxPermissions = new RxPermissions(this);//Manifest.permission.FLASHLIGHT, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_LOGS,
        rxPermissions.requestEach(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.CAMERA, android.Manifest.permission.RECORD_AUDIO, android.Manifest.permission.CALL_PHONE, Manifest.permission.BLUETOOTH)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // 用户已经同意该权限
//                            MyToast.show(LoginActivity.this,"用户已经同意该权限");
                            if (permission.name.equals(android.Manifest.permission.CALL_PHONE)){
                                androidFirstOpen();
                            }
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                            MyToast.show(GuideActivity.this, "用户拒绝了该权限");
                        } else {
                            // 用户拒绝了该权限，并且选中『不再询问』，提醒用户手动打开权限
                            MyToast.show(GuideActivity.this, "权限被拒绝，请在设置里面开启相应权限，若无相应权限会影响使用");
                        }
                    }
                });
    }


    /**用户首次打开App
     *
     */
    private void androidFirstOpen(){
        boolean notFirstOpenApp = SPUtil.booleanDataOut(SPConstant.NOT_FIRST_OPEN_APP);
        if (!notFirstOpenApp){

            LogUtil.e("去上传信息啦");
            Map<String, String> map = new HashMap<>();
            String imei = MobileInfoUtil.getIMEI(this);
            String imeiMD5 = MD5.getStringMD5(imei);

            String mac = MobileInfoUtil.getAndroidMacAddress(this);
            String mactrim = mac.replace(":" , "");
            String macMd5 = MD5.getStringMD5(mactrim);

            String androidId = MobileInfoUtil.getAndroidId(this);

            map.put("imei", imeiMD5);
            map.put("mac", macMd5);
            map.put("androidid", androidId);

            LogUtil.e("\n"
                    +"imei = " + imei + "\n"
                    +"imeiMD5= " + imeiMD5 + "\n"
                    +"mac = " + mac + "\n"
                    +"macTrim= " + mactrim +"\n"
                    +"macMd5= " + macMd5 + "\n"
                    +"androidid= " + androidId);

            Observable observable = ApiFactory.getEducationApiSingleton().androidFirstOpen(map);
            HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(this) {
                @Override
                protected void _onNext(Void v) {
                    LogUtil.e("成功");
                }

                @Override
                protected void _onError(String message) {
                    LogUtil.e("失败");
                }
            }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject, false);

            SPUtil.booleanDataIn(SPConstant.NOT_FIRST_OPEN_APP , true);
        }
    }
}
